# gem install activerecord
# gem install sqlite3-ruby (sqlite3-ruby 1.2.3 (mswin32))
begin




require 'yabzs/plugins/bzcommon'
require 'rubygems'
require 'active_record'
require 'yabzs/plugins/world_map'
require 'yabzs/db/models/game'
require 'yabzs/db/models/event'
require 'yabzs/db/models/event_type'
require 'yabzs/db/models/flag'
require 'yabzs/db/models/player'

ActiveRecord::Base.establish_connection(
      :adapter => "sqlite3",
      :dbfile  => "yabzs/var/yabzs.sqlite"
 )


include Bzflag
include BZCommon

class EventName
  SUICIDE = 'suicide'
  KILL = 'kill'
end

class FlagName
  NO = 'no'
end

$logEnabled = false;

class DatabaseLogger < Plugin

  def initialize()
    @server_start = DateTime.now
    @game = nil
    @game_start = nil
    @flags = false
    @game_end = false
  end

  def log(line)
    puts line
  end
  
  def ctf_capture(data) 
  end
  
  def player_die(data)
  begin
    if not $logEnabled: return end
    if is_server_player(data.killer_id): return end
      
    # bzflag objects
    killer = Player.new(data.killer_id)
    victim = Player.new(data.player_id)
    flagCarried = data.flag_killed_with

    if data.killer_id == data.player_id
      suicide = DB::EventType.find_by_name(EventName::SUICIDE)
		  player = DB::Player.find_by_name(killer.callsign)
		  event_datetime = DateTime.now
		  @game.events.create(:event_type => suicide, :player => player, :target => player, :datetime => event_datetime)
    else
      flag = FlagName::NO
      if (!flagCarried.empty?) 
        flag = flagCarried
      end
		  if flag != FlagName::NO:
			 @flags = true
		  end
		  
      kill = DB::EventType.find_by_name(EventName::KILL)
		  player = DB::Player.find_by_name(killer.callsign)
		  target = DB::Player.find_by_name(victim.callsign)
	
		  flag = DB::Flag.find_by_acronym(flag)
		  event_datetime = DateTime.now
		  @game.events.create(:event_type => kill, :player => player, :target => target, :datetime => event_datetime, :flag => flag)
		end
  
    if is_score_max(killer) 
          game_end_function
    end
            
  rescue Exception => e
    log e.to_s
  end
  end
  
  def player_spawn(data)
  end
  
  def chat(data)
    
  end
  
  def player_join(data)
  begin
    
     player = DB::Player.find_or_create_by_name(data.callsign) 
	   log "'#{player.name}' joined the game."
  rescue Exception => e
    log e.to_s
  end
  end
  
  def player_part(data)
    #line = "*** '#{data.callsign}' left the game."
    #log line
  end
  
  def unknown_slash_command(data)
  end
  
  def get_player_spawn_pos(data)
  end
  
  def allow_player(data)
  end

  def game_start(data)
  begin
    
    @game = DB::Game.create :datetime => DateTime.now
    png = map_png(bz_get_world_cache_data)
    @game.map = png
    log "game start at #{@game.datetime}"
  rescue Exception => e
    log e.to_s
  end
  end
  
  def game_end_function
    if not @game_end
      begin
       @game.flags = @flags
       @game.duration = (Time.now - @game.datetime.to_time).tv_sec
	     @game.save
	     log "game duration was #{@game.duration}"
	     log "game flags was #{@game.flags}"
      rescue Exception => e
        print e.to_s
      end
      @game_end = true
    end
  end
  
  
  def game_end(data)
      game_end_function
  end
  
  def tick(data)
  end
  
  def generate_world(data)
    
  end
  
  def get_player_info(data)
  end
  
  def get_auto_team(data)
  end
  
  def allow_spawn_data(data)
  end
  
  def list_server_update(data)
  end
  
  def ban(data)
  end
  
  def host_ban(data)
  end
  
  def kick(data)
  end
  
  def kill(data)
    # kiedy jest wywolywany?    
  end
  
  def player_pause(data)
  end
  
  def slash_command(data)
  end
  
  def server_msg(data)
  end
  
  def shot_fired(data)
  end
  
  def shot_ended(data)
  end
  
  def flag_grabbed(data)
  end
  
  def flag_dropped(data)
  end

  def destructor()
    game_end_function
    line = "\n------------ SERVER END [ #{@date} #{@time} ] ------------"
    log line  
      
  end

end

# new FileLogger instance
databaselogger = DatabaseLogger.new()

# register FileLogger plugin
databaselogger.register

puts "YABZS :: databaselogger"

rescue Exception => e
  puts 'Exception'
	print e.to_s
end